#include "ustandard/ustandard.h"




int main(int argc, char* argv[])
{
    if(argc != 5) {
        printf("useage : ./filesegment name outputname start-index end-index.\n");
        exit(1);
    }

    const char* path = argv[1];
    const char* path_output = argv[2];
    int start_idx = atol(argv[3]);
    int end_idx = atol(argv[4]);

    if(!(end_idx>=start_idx)) {
        printf("index param error.\n");
        exit(1);
    }

    size_t size = end_idx-start_idx+1;
    void* buf = malloc(size);

    int ret;
    int n;
    FILE* fp = fopen(path, "r");
    FILE* fpw = fopen(path_output, "w");
    ret = fseek(fp, start_idx, SEEK_SET);
    if(0 != ret) {
        printf("seek error.\n");
        exit(1);
    }
    n = fread(buf, 1, size, fp);
    if(n != size) {
        printf("read error.\n");
        exit(1);
    }
    n = fwrite(buf, 1, size, fpw);
    if(n != size) {
        printf("write error.\n");
        exit(1);
    }

    fclose(fp);
    fclose(fpw);

    return 0;

}
